package com.bawei.detail.view

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.common.event.LiveDataBus
import com.bawei.common.event.LiveDataBusEvent
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.detail.R
import com.bawei.detail.adapter.DetailPagingAdapter
import com.bawei.detail.databinding.FragmentIntroduceBinding
import com.bawei.detail.entity.AuthorEntity
import com.bawei.detail.intent.DetailViewIntent
import com.bawei.detail.uistate.DetailUiState
import com.bawei.detail.viewmodel.DetailViewModel
import com.bawei.image.ImageLoadUtil
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
class IntroduceFragment : BaseMviFragment<FragmentIntroduceBinding>() {

    private val mViewModel: DetailViewModel by activityViewModels()
    private lateinit var mPagingDataAdapter: DetailPagingAdapter

    private var userId: String = ""
    private var typeId: Int = 0

    override fun getLayoutId(): Int = R.layout.fragment_introduce

    companion object {
        @JvmStatic
        fun newInstance(userId: String, typeId: Int) =
            IntroduceFragment().apply {
                arguments = Bundle().apply {
                    putString("userId", userId)
                    putInt("typeId", typeId)
                }
            }
    }

    override fun initViews() {
        arguments?.let {
            userId = it.getString("userId").toString()
            typeId = it.getInt("typeId")
        }

        mPagingDataAdapter = DetailPagingAdapter()
        mPagingDataAdapter.setItemClickListener(object : DetailPagingAdapter.ItemClickListener{
            override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
               LiveDataBus.with<LiveDataBusEvent>("movie_detail_options").value = LiveDataBusEvent(100, entity)
            }
        })

        mDataBinding.rvUserVideo.layoutManager = LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
        mDataBinding.rvUserVideo.adapter = mPagingDataAdapter

        loadMovieList()
    }

    private fun loadMovieList(){
        lifecycleScope.launch {
            //获取同类视频
            if(typeId == -1){
                mViewModel.loadRecommendMovie().collectLatest {
                    mPagingDataAdapter.submitData(it)
                }
            }else{
                mViewModel.loadRecommendMovieById(typeId).collectLatest {
                    mPagingDataAdapter.submitData(it)
                }
            }
        }
    }


    override suspend fun sendViewIntent() {
        mViewModel.channel.send(DetailViewIntent.LoadAuthorInfo(userId))
    }

    override suspend fun collectUiState() {
       mViewModel.uiState.collect{
           when(it){
               is DetailUiState.LoadAuthorInfoSuccess -> setAuthorInfo(it.entity)
               else -> {}
           }
       }
    }

    private fun setAuthorInfo(entity: AuthorEntity) {
        ImageLoadUtil.with(requireContext()).loadUrl(entity.avatar_url).into(mDataBinding.ivUserIcon)
        mDataBinding.tvUserName.text = entity.description
        mDataBinding.tvLikeCount.text = entity.followerscount
        mDataBinding.tvSendTime.text = entity.ctime
    }


}